package com.frame.work.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.frame.work.entity.TblSecurityLog;
import com.frame.work.entity.TimeCount;
import com.frame.work.mapper.TblSecurityLogMapper;
import com.frame.work.service.TblSecurityLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author hewenze
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class TblSecurityLogServiceImpl implements TblSecurityLogService {
    private final TblSecurityLogMapper tblSecurityLogMapper;

    @Override
    public void insert(TblSecurityLog tblSecurityLog) {
        try {
            int b = tblSecurityLogMapper.insert(tblSecurityLog);
            if (b < 1) {
                log.warn("保存security日志失败:{}", tblSecurityLog);
            }
        } catch (Exception e) {
            log.warn("保存security日志失败->tblSecurityLog{},Exception:{}", tblSecurityLog, e.getCause());
        }


    }

    @Override
    public List<TblSecurityLog> selectByTime(Integer pageNum, Integer pageSize, String gatewayMac, String deviceMac, String start, String end) {

        List<TblSecurityLog> tblSecurityLogs = this.tblSecurityLogMapper.selectByTime(pageNum, pageSize, gatewayMac, deviceMac, start, end);
        return tblSecurityLogs;
    }

    @Override
    public List<TblSecurityLog> selectByToday(String gatewayMac, String deviceMac) {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String format = df.format(new Date());
        System.out.println(df.format(new Date()));
        String start = format + " 00:00:00";
        String end = format + " 23:59:59";

        List<TblSecurityLog> tblSecurityLogs = this.tblSecurityLogMapper.selectByToday(gatewayMac, deviceMac, start, end);
        System.out.println(tblSecurityLogs);

        return tblSecurityLogs;
    }

    @Override
    public List<TblSecurityLog> selectByDay(String gatewayMac, String deviceMac, long time) {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        // new Date()为获取当前系统时间
        String format = df.format(new Date(time));
        System.out.println("时间戳转换过的时间：" + df.format(new Date(time)));
        String start = format + " 00:00:00";
        String end = format + " 23:59:59";

        List<TblSecurityLog> tblSecurityLogs = this.tblSecurityLogMapper.selectByToday(gatewayMac, deviceMac, start, end);
        System.out.println(tblSecurityLogs);

        return tblSecurityLogs;
    }

    @Override
    public List<TimeCount> selectTimeCount(String gatewayMac, String deviceMac, String start, String end) {

        List<TimeCount> list = this.tblSecurityLogMapper.selectTimeCount(gatewayMac, deviceMac, start, end);
        System.out.println(list);

        return list;
    }


    @Override
    public int deleteAllByDeviceMac(String deviceMac) {
        return this.tblSecurityLogMapper.delete(Wrappers.<TblSecurityLog>lambdaQuery().eq(TblSecurityLog::getDeviceMac, deviceMac));
    }

    @Override
    public int deleteByIds(List<String> ids) {
        //查询数据库表名
        return this.tblSecurityLogMapper.deleteBatchIds(ids);
    }


}
